RAG / Retrieval-Augmented Generation
Generally, RAG is a
LLM(Large Language Model)
which can fetch data from external source(eg:
vector database, SQL Db, Graph DB, Web Search engines) & feed to AI generation
process.
Purpose of RAG? To give more Context to LLM models to predict better
What is vector?
What is Embedding Model?
RAG Pipeline
|
1. The Retrieval Phase: Chunking: Raw documents are broken down into smaller, readable pieces. Embedding: These text chunks are converted into mathematical representations (vectors) using an embedding model. Vector Search: User asks a question, system searches vector 2. The Augmentation Phase: Once the relevant information is retrieved, it isn't just displayed. It is packaged. The system takes the user’s original query and the retrieved text chunks 3. The Generation Phase This combined prompt (the user's query + the retrieved context) is fed into the LLM. This forces the model to synthesize an answer based only on the provided external data, which drastically reduces hallucinations |
RAG Flow
@startuml
actor admin as admin
box Retriever Phase #LightCyan
participant em as "from llama_index.embeddings.openai \nimport OpenAIEmbedding\n\nEmbedding Model"
participant vdb as "from llama_index.core \nimport VectorStoreIndex\n\nVector DB"
end box
box Augumentation Phase #LightYellow
participant rs as "class ResponseSynthesizer\n inside query_engine"
end box
box Generation Phase #Pink
participant llm as "llama_index.llms.openai \nimport OpenAI\n\nLLM"
end box
actor User as u
admin -> em: Feed Raw documents(data)
note over em
Create Tensors/Vectors
end note
em -> vdb: vectors
u -> rs: user_query
vdb -> rs: vectors/nodes
activate rs
rs --> rs: Create augumented_prompt \n augumented_prompt=\n (Context+user_query+vectors)
deactivate rs
rs -> llm: augumented_query
llm -> u: Response of query
@enduml
RAG Pipeline Code
User queries from security logs
We have log files(eg: VPN, firewall).
RAG pipeline will read log files and provide answers to Administrator questions.
|
Actual Code
|
Nodes created during chunking step
--- Node 1 ---
Chunk Text: 22.11.4 dst=10.1.1.20 policy=INBOUND_BLOCK
2025-05-01 11:41:55 FIREWALL_DENY src=10.1.1.6 dst=4.2.2.2 policy=OUTBOUND_BLOCK
2025-05-01 11:55:22 FIREWALL_DENY src=172.16.0.9 dst=198.51.100.5 policy=POR
Metadata: {'file_path': '/home/amit/RAG_Pipeline_Evaluation_Workbench/logs/firewall.log', 'file_name': 'firewall.log', 'file_size': 2363, 'creation_date': '2026-06-25', 'last_modified_date': '2026-06-25'}
--------------------------------
--- Node 2 ---
Chunk Text: 2025-05-01 08:01:05 VPN_LOGIN_FAILED user=john.doe ip=185.22.11.4
2025-05-01 08:03:44 VPN_LOGIN_FAILED user=john.doe ip=185.22.11.4
2025-05-01 08:06:12 VPN_LOGIN_FAILED user=john.doe ip=185.22.11.4
20
Metadata: {'file_path': '/home/amit/RAG_Pipeline_Evaluation_Workbench/logs/vpn.log', 'file_name': 'vpn.log', 'file_size': 1906, 'creation_date': '2026-06-25', 'last_modified_date': '2026-06-25'}